#include <vesa.h>
#include <types.h>


void vesa_draw_pixel(uint32_t x, uint32_t y, uint32_t color)
{
	uint32_t *address;
	
	switch(vesa_bits)
	{
		case 32:
			*address = (uint32_t) vesa_data + (uint32_t) vesa_x * (uint32_t) y * vesa_bits + (uint32_t) x * vesa_bits;
			*address = (uint32_t) color;
			break;	
			
		case 24:
			*address = (uint32_t) vesa_data + (uint32_t) vesa_x * (uint32_t) y * vesa_bits + (uint32_t) x * vesa_bits;
			*address = (uint32_t) color;
			break;	
			
		case 16:
			*address = (uint32_t) vesa_data + (uint32_t) vesa_x * (uint32_t) y * vesa_bits + (uint32_t) x * vesa_bits;
			*address = (uint16_t) color;
			break;
			
		case 8:
			*address = (uint32_t) vesa_data + (uint32_t) vesa_x * (uint32_t) y * vesa_bits + (uint32_t) x * vesa_bits;
			*address = (uint8_t) color;
			break;	
	}
	
}



void vesa_init(struct multiboot_info *multiboot)
{
	struct vbe_mode *mode_info;

	mode_info = (struct vbe_mode*) multiboot->vbe_mode_info;

	vesa_x  = mode_info->x_resolution;
	vesa_y = mode_info->y_resolution;
	vesa_bits = mode_info->bits_per_pixel;
	vesa_base = (void *) mode_info->phys_base;

	vesa_data =  (unsigned char*) vesa_base;
}


void vesa_clear()
{
	int i, j;
	
	for(j = 0; j < vesa_y; j++)
		for(i = 0; i < vesa_x; i++) 
			vesa_draw_pixel(i, j, 0);	
}
